Refactor usage polling to paged APIs with legacy fallback#138
Conversation
3ff8db6 to
07aceca
Compare
0ae6c22 to
d3ddf09
Compare
1d3000d to
1e2f1ec
Compare
1e2f1ec to
522d7f1
Compare
522d7f1 to
9417d6d
Compare
|
@seakee could u please review? |
Thanks for the PR. The current PR does help alleviate the problems of slow loading, timeouts, and high frontend aggregation pressure on the request monitoring page under large data volumes. The subsequent refresh logic fix is also valuable, as it prevents the However, this PR affects the core data path of CPA-Manager request monitoring, so I’m not recommending merging it directly just yet. A few things still need to be addressed:
|
|
thanks for your review, I will fix them |
d741d30 to
a34cfbb
Compare
|
I have implemented the suggestions above. I tested manually and it works very well, but I have not manually looked through the code, I will review them later. I have something to check with you, after using FTS 5, users cannot search with partial queries, e.g. if I want to search codex usages I cannot get it with "co" "code" etc, I have to input full query. Is this good enough for user experience? @seakee |
a34cfbb to
2f5b26c
Compare
|
First of all, thank you again for this PR and for the follow-up updates. I agree with the overall direction, and I can see that you have already addressed a number of earlier review points. Splitting the monitoring page from high-frequency polling of the full One extra note: if this PR is eventually merged into CPA-Manager, these usage aggregation/pagination APIs will very likely be referenced or adapted in CPA-Manager-Plus as well. CPA-Manager-Plus is also improving request monitoring, data summaries, caller API key statistics, and account-level statistics, so having this API design stabilized here would be directly useful there too. You are also very welcome to continue contributing PRs to CPA-Manager-Plus. Once the usage query protocol, pagination shape, FTS search behavior, and model cost aggregation are polished here, reusing them in Plus should be quite natural. That said, because this PR touches the core data path of request monitoring, I still think a few issues should be addressed before merge:
Currently the first request uses Even if the current caller always passes page 1, the helper contract is still unsafe. Please either force the first request to use
It currently merges only Also, when merging
Right now, if the summary request fails due to a network error, timeout, CORS issue, or connection failure, the status can be undefined and the code may still fall back to the legacy This can hide the real error and make refreshes slower. Please only fallback on 404/405, and rethrow all other errors.
After replacing I do not suggest reverting to
The FTS table could also use a prefix index such as
GitHub still shows a hidden/bidirectional Unicode warning. Unless there is a specific reason to keep those characters, please remove them before merge to avoid confusion in future reviews and maintenance.
The frontend payload is now paginated, which is a good improvement. However, the backend still appears to call This means large time ranges can still create backend memory and aggregation pressure. If this PR is intended as a phase-one optimization, that is acceptable, but please document the current boundary in the PR description and ideally add a larger dataset test. Longer term, accounts and api-keys should also be pushed down into SQL with
Please either add those triggers now, or at least add a clear comment that Overall, I am positive on this PR. The direction is correct, it has real value, and it aligns well with the future monitoring improvements in both CPA-Manager and CPA-Manager-Plus. However, because this may become a foundation for later request monitoring work and possible Plus-side reuse, I would prefer to polish the issues above before merging. |
|
tysm! I will search for some docs about prefix searching and try to improve it |
|
oh sorry I didn't notice that this repo was maintenance only. Do you want me to create a new PR in CPA-Manager-Plus? |
No worries, and thanks again for working on this. My plan is not to reject this PR just because CPA-Manager is mostly maintenance-only. In my view, this kind of monitoring performance improvement still falls within maintenance work, and it was already part of my original plan. This PR actually helps fill an important gap in that plan, so I still think it has value for CPA-Manager. That said, CPA-Manager-Plus is where I plan to continue more active development. It already has some optimizations around request monitoring and usage aggregation, but the implementation is still not thorough enough, and further performance work is also on the roadmap there. So my suggestion is:
|
中文
变更内容
/v0/management/usage响应拆分为更小的分页/聚合接口:summary、accounts、api-keys、realtime和models。/v0/management/usage/summary现在只返回全局聚合数据,包括总请求数、成功/失败、tokens 和 latency 聚合,不再携带详细 breakdown。/v0/management/usage数据结构。/v0/management/usage大响应。page_size硬上限,并通过白名单校验sort_key/sort_direction,避免过大分页或用户输入直接影响排序逻辑。LIKE改为 SQLite FTS5,并覆盖 API Key alias;搜索语义调整为文本 FTS 命中或 API key hash 命中,修复别名搜索会被 hash 条件误过滤的问题。end_ms被初次 render 固定的问题,避免总 tokens 和预估花费在第一次刷新后不再更新。验证
go test ./...go test ./internal/store ./internal/httpapinpm test -- --run src/features/monitoring/hooks/useUsageData.test.ts src/services/api/usageService.test.ts src/features/monitoring/hooks/useMonitoringData.test.ts src/features/monitoring/accountOverviewState.test.tsnpm test -- --run src/features/monitoring/hooks/useMonitoringData.test.tsnpm run type-checknpm run lintnpm run buildsummary/accounts/api-keys/realtime/models,不再请求完整/v0/management/usage。KongWenpeng后页面仍显示匹配调用、调用方密钥表和预估花费,不再清零。off后验证非法page_size=501和非法sort_key返回 400,合法page_size=500&sort_key=lastSeenAt返回 200。English
Changes
/v0/management/usagepayload into smaller paged/aggregate endpoints:summary,accounts,api-keys,realtime, andmodels./v0/management/usage/summarynow returns only global aggregate data, including total requests, success/failure counts, tokens, and latency aggregates, without detailed breakdowns./v0/management/usagepayload shape./v0/management/usagepayload.page_sizeand backend whitelist validation forsort_key/sort_direction, preventing oversized pages or user input from directly affecting sorting logic.LIKEmatching with SQLite FTS5 and included API Key aliases; search now matches either FTS text or API key hash, fixing alias searches that were incorrectly filtered out by the hash condition.end_mswas frozen after the first render, which could keep total tokens and estimated cost unchanged after the first refresh.Verification
go test ./...go test ./internal/store ./internal/httpapinpm test -- --run src/features/monitoring/hooks/useUsageData.test.ts src/services/api/usageService.test.ts src/features/monitoring/hooks/useMonitoringData.test.ts src/features/monitoring/accountOverviewState.test.tsnpm test -- --run src/features/monitoring/hooks/useMonitoringData.test.tsnpm run type-checknpm run lintnpm run buildsummary/accounts/api-keys/realtime/modelsand no longer calls the full/v0/management/usageendpoint.KongWenpengstill shows matching calls, the caller API key table, and estimated cost instead of clearing the page to zero.off, verified that invalidpage_size=501and invalidsort_keyreturn 400, while validpage_size=500&sort_key=lastSeenAtreturns 200.